# C:/Python27/TextResuming/model/text.py
# This is model file
#
'''A modul for text model, including sentence, paragraph, and text itself.'''
from stemming.porter2 import stem
import Phrase.phrase_chunker as phraseChunker
import os
import sys
import inspect
import nltk
import string

cmd_folder = os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0])
if cmd_folder not in sys.path:
    sys.path.insert(0, cmd_folder)
    
class title:
    '''A class for defining a title.'''
    def __init__(self, text):
        self.text = text

    def getWords(self):
        '''Return list of word.'''
        #retval = self.text.split() #using split function
        retval = nltk.word_tokenize(self.text) #using nltk
        return removeStopWord(retval) #remove stopword
        #return retval
        
    def getNumWords(self):
        '''Return num of word.'''
        return len(self.getWords())

    def getPhrase(self):
        '''Return list of phrase.'''
        return phraseChunker.extractNounPhrase(phraseChunker.chunkPhrase(phraseChunker.tagSentence(self.text)))

    def getNumPhrase(self):
        '''Return num of phrase.'''
        return len(self.getNumPhrase())

class sentence:
    '''A class for defining a sentence.'''
    def __init__(self, idx, text):
        self.idx = idx
        self.text = text

    def getText(self):
        return self.text

    def printSentence(self):
        '''Print sentence to the screen.'''
        print 'id sentence:' ,self.idx, '\ncontent : ', self.text

    def writeSentence(self, fstream):
        '''Write the sentence to a file.'''
        lines = 'id sentence:' + str(self.idx) + '\ncontent:' + self.text + '\n'
        fstream.writelines(lines)

    def getWords(self):
        '''Return list of word.'''
         #retval = self.text.split() #using split function
        retval = removePunctuation(self.text)
        #retval = nltk.word_tokenize(retval) #using nltk
        retval = self.text.split() #using split function
        #print (retval)
        
        #retval = removeStopWord(retval)
        #print removeStopWord(retval)
        return removeStopWord(retval) #remove stopword
        #return retval

    def getNumWords(self):
        '''Return num of word.'''
        return len(self.getWords())

    def getPhrase(self):
        '''Return list of phrase.'''
        return phraseChunker.extractNounPhrase(phraseChunker.chunkPhrase(phraseChunker.tagSentence(self.text)))

    def getNumPhrase(self):
        '''Return num of phrase.'''
        return len(self.getNumPhrase())

    def isPernyataan(self):
        '''Return 1 if a pernyataan, otherwise 0.'''
        pass

class paragraph:
    '''A class for defining a paragraph.'''
    def __init__(self, idx, sentences):
        self.idx = idx
        self.sentences = sentences

    def printParagraph(self):
        '''Print paragraph to the screen.'''
        print '--------------------------------------'
        print'idx paragraph :', self.idx
        for sentence in self.sentences:
            sentence.printSentence()
        print '--------------------------------------'

    def writeParagraph(self, fstream):
        '''Write the paragraph to a file.'''
        lines = 'id paragraph: ' + str(self.idx) + '\n'
        fstream.writelines(lines)
        for sentence in self.sentences:
            sentence.writeSentence(fstream)
        fstream.writelines('--------------------------------------\n')

    def getSentence(self, idSentence):
        '''Return a sentence whose index is idSentence.

            Return None if idSentence is not valid.'''
        try:
            return self.sentences[idSentence]
        except:
            return None

    def numSentences(self):
        '''Return number of sentence of a paragraph.'''
        return len(self.sentences)

class news:
    '''A class for defining a news.

        A news contains :
        0. Index
        1. Title
        2. Source
        3. Content as list of sentence.'''

    def __init__(self, idx, title, source, content):
        self.idx = idx
        self.title = title
        self.source = source
        self.content = content

    #sentence and title sector
    def getTitle(self):
        '''Return the title.'''
        return self.title

    def getSentence(self, idSentence):
        '''Return a sentence whose idx == idSentence.'''
        try:
            return self.content[idSentence]
        except Exception, e:
            print 'get sentence error', e
            return None

    def getTextSentence(self, idSentence):
        '''Return the string of a sentence.'''
        return self.getSentence(idSentence).getText()

    def getNumSentences(self):
        '''Return number of sentences in the news.'''
        return len(self.content)

    #TitleWord sector
    def getSetOfWordTitle(self):
        '''Return a set of all word that is used in the title.'''
        return set(self.title.getWords())

    def getSetOfWordSentence(self, idSentence):
        '''Return a set of all word that is used in a sentence.'''
        return set(self.getSentence(idSentence).getWords())

    def getSetOfTitleWordInSentence(self, idSentence):
        '''Return a title found in a sentence.'''
        return self.getSetOfWordTitle() & self.getSetOfWordSentence(idSentence)
    
    def getSetOfWordContent(self):
        '''Return a set of all word that is used in the sentences.'''
        retval = set()
        for sent in self.content:
            retval |= set(sent.getWords())
        return retval

    def getListOfWordContent(self):
        '''Return a list of all word that is used in the sentences.'''
        retval = []
        for sent in self.content:
            retval.extend(sent.getWords())
        return retval

    def getListOfListOfWord(self):
        '''Return a list of list of all word that is used in each sentence.'''
        retval = []
        i = 0
        for sent in self.content:
            retval.append(sent.getWords())
        return retval

    def getTitleWordInContent(self):
        '''Return the set of TitleWord that is used in the content.'''
        setContent = self.getSetOfWordContent()
        setTitle = self.getSetOfWordTitle()
        return setContent & setTitle

    def getNumTitleWordInContent(self):
        '''Return the number of TitleWord that is used in the content.'''
        return len(self.getTitleWordInContent())

    def getNumTitleWordInSentence(self, idSentence):
        '''Return Num of TitleWord which is used in a sentence.'''
        setTitleWord = self.getSetOfWordTitle()
        setSentenceWord = self.getSetOfWordSentence(idSentence)
        return len(setTitleWord & setSentenceWord)


    #TitlePhrase sector
    def getSetOfTitlePhrase(self):
        '''Return a set of Phrase in Title.'''
        return set(self.title.getPhrase())

    def getSetOfPhraseSentence(self, idSentence):
        '''Return a set of Phrase in a sentence.'''
        return set(self.getSentence(idSentence).getPhrase())

    def getSetOfTitlePhraseInSentence(self, idSentence):
        '''Return a TitlePhrase found in a sentence.'''
        return self.getSetOfTitlePhrase() & self.getSetOfPhraseSentence(idSentence)

    def getSetOfContentPhrase(self):
        '''Return a set of Phrase in content.'''
        retval = set()
        for sent in self.content:
            retval |= set(sent.getPhrase())
        return retval

    def getTitlePhraseInContent(self):
        '''Return the set of TitlePhrase that is used in the content.'''
        setContent = self.getSetOfContentPhrase()
        setTitle = self.getSetOfTitlePhrase()
        return setContent & setTitle
        
    def getNumTitlePhraseInContent(self):
        '''Return the number of TitlePhrase that is used in the content.'''
        return len(self.getTitlePhraseInContent())
    
    def getNumTitlePhraseInSentence(self, idSentence):
        '''Return Num of TitlePhrase which is used in a sentence.'''
        setTitlePhrase = self.getSetOfTitlePhrase()
        setSentencePhrase = self.getSetOfPhraseSentence(idSentence)
        return len(setTitlePhrase & setSentencePhrase)

class text:
    '''A class for defining a text.

        A text contain some paragraphs.'''
    
    def __init__(self, idx, paragraphs):
        self.idx = idx
        self.paragraphs = paragraphs

    def printText(self):
        '''Print text to the screen.'''
        print 'idx text : ', self.idx
        for paragraph in self.paragraphs:
            paragraph.printParagraph()

    def writeText(self, fstream):
        '''Write the paragraph to a file.'''
        lines = 'id text : ' + str(self.idx) + '\n'
        fstream.writelines(lines)
        for paragraph in self.paragraphs:
            paragraph.writeParagraph(fstream)
        
    def numParagraph(self):
        '''Return number of paragraphs of a text.'''
        return len(self.paragraphs)

    def getParagraph(self, idParagraph):
        '''Return a paragraph whose index is idParagraph.

            Return None if idParagraph is not valid.'''
        try:
            return self.paragraphs[idParagraph]
        except:
            return None

def removeStopWord(listWord):
    '''Removing stop word from a list of word.'''
    stopWords = nltk.corpus.stopwords.words('english')
    extraStopWords = ['the', 'is', 'am', 'are', 'of', 'its', 'to']
    stopWords.extend(extraStopWords)
    for stopWord in stopWords:
        if stopWord in listWord:
            listWord = remove_values_from_list(listWord, stopWord)
    return listWord

def remove_values_from_list(the_list, val):
    return [value for value in the_list if value != val]

def removePunctuation(stringAbsurd):
    '''Removing punctuation from a string.'''
    for punct in string.punctuation:
        stringAbsurd = stringAbsurd.replace(punct, '')
    return stringAbsurd

def createParagraph(startId, paragraphId, sentences):
    '''Creating paragraph object from list of string.

        startId is the first id of the new list of sentence'''
    i = startId
    newSentences = []
    for sent in sentences:
        s = sentence(i, sent)
        newSentences.append(s)
        i += 1
    return paragraph(paragraphId, newSentences)

def main():
    cmd_folder = os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0])
    if cmd_folder not in sys.path:
        sys.path.insert(0, cmd_folder)
    print cmd_folder
    
    if __name__ == '__main__':
        print 'run as main program'
        s1 = sentence(0, 'kalimat pertama')
        s2 = sentence(1, 'kalimat ke dua')

        sentences = [s1, s2]
        p1 = paragraph(0, sentences)
        p1.printParagraph()
    
    else:
        print 'run as modul'
        print __doc__

def main2():
    a = "Seattle-based Aviation Partners (API), patent-holder of the blended winglet, is seeking the dismissal of an Airbus lawsuit aiming to clarify the intellectual property rights of the European airframer's alleged use of API's winglet on the Airbus A320's sharklets."
    b = "Air New Zealand currently serves as launch customer for the sharklets, and anticipates the delivery of its first A320 with winglets later this year."
    c = "The implications of the lawsuit filed by Airbus could nullify any royalty claims by API, if Airbus's sharklet design is found to use API's intellectual property."

    judul = "Aviation Partners seeks dismissal of Airbus winglet lawsuit"

    sent_a = sentence(0, a)
    sent_b = sentence(1, b)
    sent_c = sentence(2, c)

    listWord = sent_a.getWords()
    print removeStopWord(listWord)

    titleNews = title(judul)

    content = [sent_a, sent_b, sent_c]
    
    source = "_source_"

    thisNews = news(0, titleNews, source, content)
    
    print thisNews.getSentence(0).text


    # print thisNews.getNumTitleWordInContent()
    # print thisNews.getTitleWordInContent()
    # print thisNews.title.getWords()

    print thisNews.getSetOfTitlePhrase()
    print thisNews.getSetOfContentPhrase()
    print thisNews.getNumTitlePhraseInContent()

    print 'fin'

if __name__ == '__main__':
        main2()
